#include "ptitlebar.h"
#include <QSizePolicy>

PTitleBar::PTitleBar(QWidget *parent)
 : QFrame(parent)
{
   
  m_hboxLayout = new QHBoxLayout(this);
  
  m_hboxLayout->setSpacing(0);
  m_hboxLayout->setMargin(0);
  m_hboxLayout->setObjectName(QString::fromUtf8("hboxLayout"));
  
  lblTitle = new QLabel(this);
  lblTitle->setObjectName(QString::fromUtf8("lblTitle"));
  //lblTitle->setPixmap(QPixmap(QString::fromUtf8(":/res/title.png")));
  m_hboxLayout->addWidget(lblTitle);
//  qDebug("titlebar construnctor 1");
  m_spacerItem = new QSpacerItem(381, 16, QSizePolicy::Expanding, QSizePolicy::Minimum);
  m_hboxLayout->addItem(m_spacerItem);
  m_btnMin = new PStyledButton(this);
  m_btnMin->setNumStates(2);
  m_btnMin->setObjectName(QString::fromUtf8("m_btnMin"));
  QSizePolicy sizePolicy3(static_cast<QSizePolicy::Policy>(0), static_cast<QSizePolicy::Policy>(0));
  sizePolicy3.setHorizontalStretch(0);
  sizePolicy3.setVerticalStretch(0);
  sizePolicy3.setHeightForWidth(m_btnMin->sizePolicy().hasHeightForWidth());
  m_btnMin->setSizePolicy(sizePolicy3);
  m_btnMin->setMaximumSize(QSize(17, 17));
//  qDebug("titlebar construnctor 2");
  //m_btnMin->setIcon(QIcon(QString::fromUtf8(":/res/panel_background.png")));
  //m_btnMin->setProperty("m_adjustToPixmap", QVariant(true));
  m_hboxLayout->addWidget(m_btnMin);
  m_btnMax = new PStyledButton(this);
  m_btnMax->setNumStates(2);
  m_btnMax->setObjectName(QString::fromUtf8("m_btnMax"));
  QSizePolicy sizePolicy4(static_cast<QSizePolicy::Policy>(0), static_cast<QSizePolicy::Policy>(0));
  sizePolicy4.setHorizontalStretch(0);
  sizePolicy4.setVerticalStretch(0);
  sizePolicy4.setHeightForWidth(m_btnMax->sizePolicy().hasHeightForWidth());
  m_btnMax->setSizePolicy(sizePolicy4);
  m_btnMax->setMaximumSize(QSize(17, 17));
  //m_btnMax->setIcon(QIcon(QString::fromUtf8(":/res/bt_maximize.png")));
  m_btnMax->setProperty("m_adjustToPixmap", QVariant(true));
//  qDebug("titlebar construnctor 3");
  m_hboxLayout->addWidget(m_btnMax);
  m_btnClose = new PStyledButton(this);
  m_btnClose->setNumStates(2);
  m_btnClose->setObjectName(QString::fromUtf8("m_btnClose"));
  QSizePolicy sizePolicy5(static_cast<QSizePolicy::Policy>(0), static_cast<QSizePolicy::Policy>(0));
  sizePolicy5.setHorizontalStretch(0);
  sizePolicy5.setVerticalStretch(0);
  sizePolicy5.setHeightForWidth(m_btnClose->sizePolicy().hasHeightForWidth());
  m_btnClose->setSizePolicy(sizePolicy5);
  m_btnClose->setMaximumSize(QSize(17, 17));
  //m_btnClose->setIcon(QIcon(QString::fromUtf8(":/res/bt_close.png")));
//  qDebug("titlebar construnctor 4");
  m_hboxLayout->addWidget(m_btnClose);
  connect(m_btnMin,SIGNAL(clicked()),this,SLOT(minimizeParent()));
  connect(m_btnClose,SIGNAL(clicked()),this,SLOT(closeParent()));
  connect(m_btnMax,SIGNAL(clicked()),this,SLOT(restoreParent()));
}


PTitleBar::~PTitleBar()
{
}




/*!
    \fn PTitleBar::setMinimizePixmap(QPixmap &pixmap)
 */
void PTitleBar::setMinimizePixmap(QPixmap pixmap,int state)
{
//    qDebug("PTitleBar::setMinimizePixmap");
    m_btnMin->replaceStatePixmap(state,pixmap);
    for(int i=1;i<5;++i)
     if(m_btnMin->getStatePixmap(i).isNull())
      m_btnMin->replaceStatePixmap(i,m_btnMin->getStatePixmap(i-1));
    if(m_btnMin->getStatePixmap(0).isNull()) m_btnMin->replaceStatePixmap(0,pixmap);
}


void PTitleBar::setMaximizePixmap(QPixmap pixmap,int state)
{
//  qDebug("PTitleBar::setMaximizePixmap");
  m_btnMax->replaceStatePixmap( state, pixmap );
  for(int i=1;i<5;++i)
     if(m_btnMax->getStatePixmap(i).isNull())
      m_btnMax->replaceStatePixmap(i,m_btnMax->getStatePixmap(i-1));
    if(m_btnMax->getStatePixmap(0).isNull()) m_btnMax->replaceStatePixmap(0,pixmap);
}




/*!
    \fn PTitleBar::getMinimizePixmap()
 */
QPixmap PTitleBar::getMinimizePixmap(int state)
{
//   qDebug("PTitleBar::getMinimizePixmap");
   //const QPixmap p = m_btnMin->getStatePixmap( state );
   //QPixmap pp = p;
   //qDebug("p");
   return m_btnMin->getStatePixmap( state );
}


/*!
    \fn PTitleBar::getMaximizePixmap()
 */
QPixmap PTitleBar::getMaximizePixmap(int state)
{
//   qDebug("PTitleBar::getMaximizePixmap");
   return m_btnMax->getStatePixmap( state );
}


/*!
    \fn PTitleBar::getClosePixmap()
 */
 QPixmap  PTitleBar::getClosePixmap(int state)
{
//    qDebug("PTitleBar::getClosePixmap");
    return m_btnClose->getStatePixmap( state );
}


/*!
    \fn PTitleBar::getTitlePixmap()
 */
 QPixmap    PTitleBar::getTitlePixmap()
{
//    qDebug("PTitleBar::getTitlePixmap");
    //QPixmap empty;
    if(!lblTitle->pixmap())
    {
    // qDebug("invalid pixmap");
     return  QPixmap();
    } 
//    qDebug("not null");
    return *lblTitle->pixmap();
}


/*!
    \fn PTitleBar::getTitleString()
 */
 QString   PTitleBar::getTitleString()
{
//   qDebug("PTitleBar::getTitleString");
   return lblTitle->text();
}


/*!
    \fn PTitleBar::setClosePixmap(QPixmap &pixmap)
 */
void PTitleBar::setClosePixmap(QPixmap pixmap,int state)
{
//    qDebug("PTitleBar::setClosePixmap");
    m_btnClose->replaceStatePixmap(state, pixmap);
    for(int i=1;i<5;++i)
     if(m_btnClose->getStatePixmap(i).isNull())
      m_btnClose->replaceStatePixmap(i,m_btnClose->getStatePixmap(i-1));
    if(m_btnClose->getStatePixmap(0).isNull()) m_btnClose->replaceStatePixmap(0,pixmap);
}


/*!
    \fn PTitleBar::setTitlePixmap(QPixmap &pixmap)
 */
void PTitleBar::setTitlePixmap(QPixmap pixmap)
{
//    qDebug("PTitleBar::setTitlePixmap");
    lblTitle->setPixmap(pixmap);
//    qDebug("end PTitleBar::setTitlePixmap");
}


/*!
    \fn PTitleBar::setTitleString(QString &title)
 */
void PTitleBar::setTitleString(QString title)
{
//    qDebug("PTitleBar::setTitleString");
    lblTitle->setText(title);
}




/*!
    \fn PTitleBar::restoreParent()
 */
void PTitleBar::restoreParent()
{
//    qDebug("PTitleBar::restoreParent");
    if(parentWidget()->windowState() == Qt::WindowMaximized)
     parentWidget()->showNormal();
    else parentWidget()->showMaximized();
}


/*!
    \fn PTitleBar::closeParent()
 */
void PTitleBar::closeParent()
{
//    qDebug("close parent");
    parentWidget()->close();
}


/*!
    \fn PTitleBar::minimizeParent()
 */
void PTitleBar::minimizeParent()
{
//    qDebug("PTitleBar::minimizeParent");
    parentWidget()->showMinimized();
}
